package com.coldchain.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.common.util.FunctionUtils;
import com.coldchain.common.util.ValidateUtil;
import com.coldchain.order.entity.BizDispatchingOrder;
import com.coldchain.order.entity.PlanOrder;
import com.coldchain.order.mapper.PlanOrderMapper;
import com.coldchain.order.service.IPlanOrderService;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fangliai
 * @since 2018-10-17
 */
@Service
public class PlanOrderServiceImpl extends ServiceImpl<PlanOrderMapper, PlanOrder> implements IPlanOrderService {

	@SuppressWarnings("unchecked")
	@Override
	public ObjectRestResponse<String> savePlan(PlanOrder vo) {
		if(!ValidateUtil.stringEmpty(vo.getOrderId())){
			return FunctionUtils.getFailRest("你牛逼业务订单号都被你搞没了");
		}else if(!ValidateUtil.stringEmpty(vo.getOrderId())){
			return FunctionUtils.getFailRest("你牛逼订单号都被你搞没了");
		}else if(!ValidateUtil.stringEmpty(vo.getUserId())){
			return FunctionUtils.getFailRest("你牛逼账号都被你搞没了");
		}else if(ValidateUtil.Objectempty(vo.getPlanType())){
			return FunctionUtils.getFailRest("选择一下类型");
		}else if(!ValidateUtil.isEquals(1,vo.getPlanType())){
			if(!ValidateUtil.stringEmpty(vo.getDay())){
				return FunctionUtils.getFailRest("请选择时间");
			}
		}
		QueryWrapper<BizDispatchingOrder> bizWrapper = new QueryWrapper<BizDispatchingOrder>();
		bizWrapper.eq("order_id", vo.getOrderId());
//		BizDispatchingOrdercao bizorder = bizDispatchingOrderService.getOne(bizWrapper);
//		if(bizorder==null){
//			return FunctionUtils.getFailRest("未找到订单记录");
//		}
//		vo.setBizId(bizorder.getBizId());
		QueryWrapper<PlanOrder> queryWrapper = new QueryWrapper<PlanOrder>();
		queryWrapper.eq("user_id",vo.getPlanId()).eq("biz_id", vo.getBizId())
		.eq("plan_type", vo.getPlanType()).eq("day", vo.getDay());
		PlanOrder planOrder = super.getOne(queryWrapper);
		if(!ValidateUtil.Objectempty(planOrder)){
			return FunctionUtils.getFailRest("计划已经存在");
		}
		boolean saveflag = super.save(vo);
		if(saveflag){
			return FunctionUtils.getSuccessRest("已计划订单，明天开始生效");
		}
		return FunctionUtils.getFailRest("内部错误，请重试");
	}

	@SuppressWarnings("unchecked")
	@Override
	public ObjectRestResponse<String> removePlan(String userId, Integer planId) {
		QueryWrapper<PlanOrder> queryWrapper = new QueryWrapper<PlanOrder>();
		queryWrapper.eq("user_id", userId).eq("plan_id", planId);
		boolean removeflag = super.remove(queryWrapper);
		if(removeflag){
			return FunctionUtils.getSuccessRest("计划订单已删除");
		}
		return FunctionUtils.getFailRest("没有找到计划订单");
	}

	@Override
	public IPage<PlanOrder> listPlan(Page<PlanOrder> page, PlanOrder vo) {
		QueryWrapper<PlanOrder> queryWrapper = new QueryWrapper<>(vo);
		queryWrapper.eq("user_id", vo.getUserId());
		if(ValidateUtil.stringEmpty(vo.getOrderId())){
			queryWrapper.eq("order_id", vo.getOrderId());
		}else if(ValidateUtil.stringEmpty(vo.getOrderId())){
			queryWrapper.eq("biz_id", vo.getBizId());
		}else if(ValidateUtil.stringEmpty(vo.getUserId())){
			queryWrapper.eq("plan_type", vo.getPlanType());
		}
		IPage<PlanOrder> list = super.page(page, queryWrapper);
		return list;
	}
}
